



#include "..\Headers\BC_Debugger.h"
#include <Commdlg.h>
#include <commctrl.h >

#pragma comment( lib, "comctl32.lib") 

HWND BCDebugger::listView = NULL;
HWND BCDebugger::debugWindow = NULL;
int BCDebugger::maxMessages = 50;




void BCDebugger::init()
{

	INITCOMMONCONTROLSEX icex;
    icex.dwICC = ICC_LISTVIEW_CLASSES;
    InitCommonControlsEx(&icex);

	WNDCLASSEX wcex;
	ZeroMemory(&wcex,sizeof(WNDCLASSEX));
	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_DBLCLKS;
	wcex.lpfnWndProc	= DebugWindowCallback;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hIcon			= 0;
	wcex.hCursor		= LoadCursor(0,IDC_ARROW);
	wcex.hInstance      = GetModuleHandle(0);
	wcex.hbrBackground	= (HBRUSH)GetStockObject(LTGRAY_BRUSH);
	wcex.lpszMenuName	= (LPCTSTR)0;
	wcex.lpszClassName	= "BATTLECITYDEBUGWINDOW";
	wcex.hIconSm		= 0;

	if (!RegisterClassEx(&wcex))
	{
		MessageBox(0,"RegisterClassEx : Failed to register debug window class !","Error",MB_OK|MB_ICONERROR);
	}


	debugWindow = CreateWindowEx(0,"BATTLECITYDEBUGWINDOW","BattleCity Debug Window",WS_CAPTION|WS_VISIBLE,10,10,500,500,0,0,GetModuleHandle(0),0);
	
	if (debugWindow) 
	{
		listView = CreateWindow(WC_LISTVIEW,"",WS_CHILD |WS_VISIBLE|LVS_REPORT|LVS_SINGLESEL|LVS_SHOWSELALWAYS , 0,0,500,500,debugWindow,0,GetModuleHandle(0),0);
		if (listView) 
		{
			LV_COLUMN  lvc;
			memset(&lvc,0,sizeof(lvc));                 
			lvc.mask=LVCF_TEXT|LVCF_SUBITEM|LVCF_WIDTH;
			lvc.pszText= "Message :";
			lvc.cx = 400;
			SendMessage(listView,LVM_INSERTCOLUMN,0,(LPARAM)&lvc);
			
			lvc.cx = 100;
			lvc.pszText= "Level :";
			SendMessage(listView,LVM_INSERTCOLUMN,0,(LPARAM)&lvc);
			
			HFONT defaultFont;
			defaultFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
			SendMessage(listView, WM_SETFONT, WPARAM (defaultFont), TRUE);

			SendMessage(listView,LVM_SETEXTENDEDLISTVIEWSTYLE,0,LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);


			std::stringstream ss;

			SYSTEMTIME stime;
			GetLocalTime(&stime);

			ss << "Battle City Debugger (ver : 0.1) Started at : " <<  stime.wYear << "." << stime.wMonth << "." << stime.wDay << "  " << stime.wHour << ":" << stime.wMinute << ":" << stime.wSecond;


			insertItem("Info : " , ss.str());


		}
		else
		{
			MessageBox(0,"Debugger listView creation failed !","Error",MB_OK|MB_ICONERROR);
		}
	}
	else
	{
		MessageBox(0,"Could Not Create debug window","Error",MB_OK|MB_ICONERROR);
	}

}




void BCDebugger::error(const std::string& text)
{
	insertItem("Error : ",text);
}

void BCDebugger::warning(const std::string& text)
{
	insertItem("Warning : ",text);
}

void BCDebugger::info_s(const std::string& text)
{
	insertItem("Log : ",text);
}


void BCDebugger::info_f(float value)
{
	std::stringstream ss;
	ss << value;
	insertItem("Log : ",ss.str());
}


void BCDebugger::checkError(bool condition, const std::string &text)
{
	if (!condition)
		insertItem("Error : ",text);
}


void BCDebugger::clearList()
{
	ListView_DeleteAllItems(listView);
}

void BCDebugger::insertItem(const std::string& level , const std::string& text)
{
	if (ListView_GetItemCount(listView) > maxMessages) ListView_DeleteAllItems(listView);

	LVITEM newItem;
	memset(&newItem,0,sizeof(newItem));
	newItem.mask=LVIF_TEXT;
	newItem.iItem= ListView_GetItemCount(listView) + 1;
	newItem.iSubItem= 0;
	newItem.pszText= (char*) level.c_str();
	
	int item = ListView_InsertItem(listView,&newItem);


	memset(&newItem,0,sizeof(newItem));

	newItem.mask=LVIF_TEXT;
	newItem.iItem = item;
	newItem.iSubItem= 1;
	newItem.pszText= (char*) text.c_str();
	
	ListView_SetItem(listView,&newItem);

}








void BCDebugger::clearAfter(int numMessages)
{
	maxMessages = numMessages;
}


void BCDebugger::destroy()
{
	if (IsWindow(debugWindow)) DestroyWindow(debugWindow);
	UnregisterClass("BATTLECITYDEBUGWINDOW",GetModuleHandle(0));
}











LRESULT BCDebugger::DebugWindowCallback(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

	if (msg == WM_SIZE)
	{
		SetWindowPos(listView,0,0,0,GET_X_LPARAM(lParam) , GET_Y_LPARAM(lParam),0);
		return 0;

	}




	return DefWindowProc(hwnd, msg, wParam, lParam);

}


































